package com.zhwmall.product.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhwmall.common.entity.coupon.SpuBoundsEntity;
import com.zhwmall.common.entity.product.*;
import com.zhwmall.common.to.SkuReductionTo;
import com.zhwmall.common.utils.R;
import com.zhwmall.common.vo.spuSaveVo.Attr;
import com.zhwmall.common.vo.spuSaveVo.Images;
import com.zhwmall.common.vo.spuSaveVo.Skus;
import com.zhwmall.common.vo.spuSaveVo.SpuSaveVo;
import com.zhwmall.product.dao.SpuInfoDao;
import com.zhwmall.product.feign.CouponFeignService;
import com.zhwmall.product.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service("spuInfoService")
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoDao, SpuInfoEntity> implements SpuInfoService {

    @Autowired
    private SpuInfoDescService spuInfoDescService;

    @Autowired
    private SpuImagesService spuImagesService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    @Autowired
    private CouponFeignService couponFeignService;

    @Autowired
    private SkuInfoService skuInfoService;

    @Autowired
    private SkuImagesService skuImagesService;

    @Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;

    @Transactional
    @Override
    public void saveSpuInfo(SpuSaveVo spuSaveVo) {
        //1、保存spu基本信息 pms_spu_info
        SpuInfoEntity spuInfoEntity = new SpuInfoEntity();
        BeanUtils.copyProperties(spuSaveVo, spuInfoEntity);
        spuInfoEntity.setPublishStatus(1);
        spuInfoEntity.setCreateTime(new Date());
        spuInfoEntity.setUpdateTime(new Date());
        baseMapper.insert(spuInfoEntity);

        //2、保存Spu的描述图片 pms_spu_info_desc
        SpuInfoDescEntity descEntity = new SpuInfoDescEntity();
        descEntity.setSpuId(spuInfoEntity.getId());
        descEntity.setDecript(String.join(",", spuSaveVo.getDecript()));
        spuInfoDescService.saveIntroduce(descEntity);

        //3、保存spu的图片集 pms_spu_images
        spuImagesService.saveImages(spuInfoEntity.getId(), spuSaveVo.getImages());

        //4、保存spu的规格参数;pms_product_attr_value
        productAttrValueService.saveProductAttrValue(spuInfoEntity.getId(), spuSaveVo.getBaseAttrs());

        //5、保存spu的积分信息；zhwmall_sms->sms_spu_bounds
        SpuBoundsEntity boundsEntity = new SpuBoundsEntity();
        BeanUtils.copyProperties(spuSaveVo.getBounds(), boundsEntity);
        R boundsCode = couponFeignService.saveSpuBoundsEntity(boundsEntity);

        if ((Integer) boundsCode.get("code") != 0) {
            System.out.println("远程保存spu积分信息失败");
        }

        //6、保存当前spu对应的所有sku信息；
        List<Skus> skus = spuSaveVo.getSkus();
        if (CollUtil.isNotEmpty(skus)) {
            skus.forEach(sku -> {
                String defaultImg = null;
                for (Images image : sku.getImages()) {
                    if (image.getDefaultImg() == 1) {
                        defaultImg = image.getImgUrl();
                    }
                }
                SkuInfoEntity skuInfoEntity = new SkuInfoEntity();
                BeanUtils.copyProperties(sku, skuInfoEntity);
                skuInfoEntity.setBrandId(spuInfoEntity.getBrandId());
                skuInfoEntity.setCatalogId(spuInfoEntity.getCatalogId());
                skuInfoEntity.setSaleCount(0L);
                skuInfoEntity.setSpuId(spuInfoEntity.getId());
                skuInfoEntity.setSkuDefaultImg(defaultImg);

                //6.1）、sku的基本信息；pms_sku_info
                skuInfoService.saveSkuInfo(skuInfoEntity);

                Long skuId = skuInfoEntity.getSkuId();

                //6.2）、sku的图片信息；pms_sku_image
                List<SkuImagesEntity> imagesEntities = sku.getImages().stream().map(img -> {
                    SkuImagesEntity skuImagesEntity = new SkuImagesEntity();
                    skuImagesEntity.setSkuId(skuId);
                    skuImagesEntity.setImgUrl(img.getImgUrl());
                    skuImagesEntity.setDefaultImg(img.getDefaultImg());
                    return skuImagesEntity;
                }).filter(item -> StrUtil.isNotEmpty(item.getImgUrl())).collect(Collectors.toList());

                skuImagesService.saveBatchImg(imagesEntities);

                //6.3）、sku的销售属性信息：pms_sku_sale_attr_value
                List<Attr> attrs = sku.getAttr();
                List<SkuSaleAttrValueEntity> skuSaleAttrValueEntities = attrs.stream().map(attr -> {
                    SkuSaleAttrValueEntity attrValueEntity = new SkuSaleAttrValueEntity();
                    BeanUtils.copyProperties(attr, attrValueEntity);
                    attrValueEntity.setSkuId(skuId);

                    return attrValueEntity;
                }).collect(Collectors.toList());
                skuSaleAttrValueService.saveBatchAttr(skuSaleAttrValueEntities);

                //6.4）、sku的优惠、满减等信息；zhwmall_sms->sms_sku_ladder\sms_sku_full_reduction\sms_member_price
                SkuReductionTo skuReductionTo = new SkuReductionTo();
                BeanUtils.copyProperties(sku, skuReductionTo);
                skuReductionTo.setSkuId(skuId);
                if (skuReductionTo.getFullCount() > 0 || skuReductionTo.getFullPrice().compareTo(new BigDecimal("0")) == 1) {
                    R reductionCode = couponFeignService.saveSkuReduction(skuReductionTo);
                    if ((Integer) reductionCode.get("code") != 0) {
                        System.out.println("远程保存sku优惠信息失败");
                    }
                }
            });
        }
    }
}